<?php

class CanvasController extends Controller {

    public function actionIndex() {
        $this->render('index');
    }

    public function actionUpload() {
        $basePath = Yii::app()->basePath . '/../';
        $upload = 'uploads/';

        if (!is_dir($basePath . $upload)) {
            mkdir($basePath . $upload, 0777);
            mkdir($basePath . $upload . 'temp/', 0777);
        }

        $dir = date("Ym");
        $subDir = date("d");

        if (!is_dir($basePath . $upload . $dir)) {
            mkdir($basePath . $upload . $dir, 0777);
        }

        if (!is_dir($basePath . $upload . $dir . '/' . $subDir)) {
            mkdir($basePath . $upload . $dir . '/' . $subDir, 0777);
        }
        
        $fileName = $saveDir = $upload . $dir . '/' . $subDir . "/".uniqid().'.png';
        
        $img = Yii::app()->request->getParam('image');
        $content = base64_decode(substr($img, 22));
        file_put_contents($fileName, $content);
        echo json_encode(array('src' =>$fileName));
    }
    
    public function actionText() {
        $this->render('text');
    }

}
